﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DDD.Core;

namespace DDDTest.Core {
    [TestClass]
    public class TestImageWriter {

        #region Method(PaintWhite)
        void PaintWhite (Image2D<float> img) {
            var width = img.Width;
            var height = img.Height;
            var cpp = img.Cpp;

            var pixels = new float[width * height * cpp];
            for (var y = 0; y < width; y++) {
                for (var x = 0; x < height; x++) {
                    for (var i = 0; i < cpp; i++) {
                        pixels[(y * width + x) * cpp + i] = 1.0f;
                    }
                }
            }
            img.Set (0, 0, width, height, pixels);
        }

        void PaintWhite (Image2D<sbyte> img) {
            var width = img.Width;
            var height = img.Height;
            var cpp = img.Cpp;

            var pixels = new sbyte[width * height * cpp];
            for (var y = 0; y < width; y++) {
                for (var x = 0; x < height; x++) {
                    for (var i = 0; i < cpp; i++) {
                        pixels[(y * width + x) * cpp + i] = 127;
                    }
                }
            }
            img.Set (0, 0, width, height, pixels);
        }

        void PaintWhite (Image2D<byte> img) {
            var width = img.Width;
            var height = img.Height;
            var cpp = img.Cpp;

            var pixels = new byte[width * height * cpp];
            for (var y = 0; y < width; y++) {
                for (var x = 0; x < height; x++) {
                    for (var i = 0; i < cpp; i++) {
                        pixels[(y * width + x) * cpp + i] = 255;
                    }
                }
            }
            img.Set (0, 0, width, height, pixels);
        }

        void PaintWhite (Image2D<short> img) {
            var width = img.Width;
            var height = img.Height;
            var cpp = img.Cpp;

            var pixels = new short[width * height * cpp];
            for (var y = 0; y < width; y++) {
                for (var x = 0; x < height; x++) {
                    for (var i = 0; i < cpp; i++) {
                        pixels[(y * width + x) * cpp + i] = 32767;
                    }
                }
            }
            img.Set (0, 0, width, height, pixels);
        }

        void PaintWhite (Image2D<ushort> img) {
            var width = img.Width;
            var height = img.Height;
            var cpp = img.Cpp;

            var pixels = new ushort[width * height * cpp];
            for (var y = 0; y < width; y++) {
                for (var x = 0; x < height; x++) {
                    for (var i = 0; i < cpp; i++) {
                        pixels[(y * width + x) * cpp + i] = 65535;
                    }
                }
            }
            img.Set (0, 0, width, height, pixels);
        }

        void PaintWhite (Image2D<int> img) {
            var width = img.Width;
            var height = img.Height;
            var cpp = img.Cpp;

            var pixels = new int[width * height * cpp];
            for (var y = 0; y < width; y++) {
                for (var x = 0; x < height; x++) {
                    for (var i = 0; i < cpp; i++) {
                        pixels[(y * width + x) * cpp + i] = 32767;
                    }
                }
            }
            img.Set (0, 0, width, height, pixels);
        }

        void PaintWhite (Image2D<uint> img) {
            var width = img.Width;
            var height = img.Height;
            var cpp = img.Cpp;

            var pixels = new uint[width * height * cpp];
            for (var y = 0; y < width; y++) {
                for (var x = 0; x < height; x++) {
                    for (var i = 0; i < cpp; i++) {
                        pixels[(y * width + x) * cpp + i] = 65535;
                    }
                }
            }
            img.Set (0, 0, width, height, pixels);
        }

        void PaintBlack (Image2D<float> img) {
            var width = img.Width;
            var height = img.Height;
            var cpp = img.Cpp;

            var pixels = new float[width * height * cpp];
            for (var y = 0; y < width; y++) {
                for (var x = 0; x < height; x++) {
                    for (var i = 0; i < cpp; i++) {
                        pixels[(y * width + x) * cpp + i] = 0;
                    }
                }
            }
            img.Set (0, 0, width, height, pixels);
        }

        #endregion


        #region TestMethod
        /// <summary>
        /// 画像の保存のテスト。全部
        /// </summary>
        [TestMethod]
        public void Test_Save () {
            var img_R8 = new Image2D<float> (PixelFormat.R8, 320, 320);
            var img_R8i = new Image2D<sbyte> (PixelFormat.R8i, 320, 320);
            var img_R8ui = new Image2D<byte> (PixelFormat.R8ui, 320, 320);
            var img_R16 = new Image2D<float> (PixelFormat.R16, 320, 320);
            var img_R16f = new Image2D<float> (PixelFormat.R16f, 320, 320);
            var img_R16i = new Image2D<short> (PixelFormat.R16i, 320, 320);
            var img_R16ui = new Image2D<ushort> (PixelFormat.R16ui, 320, 320);
            var img_R32f = new Image2D<float> (PixelFormat.R32f, 320, 320);
            var img_R32i = new Image2D<int> (PixelFormat.R32i, 320, 320);
            var img_R32ui = new Image2D<uint> (PixelFormat.R32ui, 320, 320);

            var img_RG8 = new Image2D<float> (PixelFormat.RG8, 320, 320);
            var img_RG8i = new Image2D<sbyte> (PixelFormat.RG8i, 320, 320);
            var img_RG8ui = new Image2D<byte> (PixelFormat.RG8ui, 320, 320);
            var img_RG16 = new Image2D<float> (PixelFormat.RG16, 320, 320);
            var img_RG16f = new Image2D<float> (PixelFormat.RG16f, 320, 320);
            var img_RG16i = new Image2D<short> (PixelFormat.RG16i, 320, 320);
            var img_RG16ui = new Image2D<ushort> (PixelFormat.RG16ui, 320, 320);
            var img_RG32f = new Image2D<float> (PixelFormat.RG32f, 320, 320);
            var img_RG32i = new Image2D<int> (PixelFormat.RG32i, 320, 320);
            var img_RG32ui = new Image2D<uint> (PixelFormat.RG32ui, 320, 320);

            var img_RGB8 = new Image2D<float> (PixelFormat.RGBA8, 320, 320);
            var img_RGB8i = new Image2D<sbyte> (PixelFormat.RGBA8i, 320, 320);
            var img_RGB8ui = new Image2D<byte> (PixelFormat.RGBA8ui, 320, 320);
            var img_RGB16 = new Image2D<float> (PixelFormat.RGBA16, 320, 320);
            var img_RGB16f = new Image2D<float> (PixelFormat.RGBA16f, 320, 320);
            var img_RGB16i = new Image2D<short> (PixelFormat.RGBA16i, 320, 320);
            var img_RGB16ui = new Image2D<ushort> (PixelFormat.RGBA16ui, 320, 320);
            var img_RGB32f = new Image2D<float> (PixelFormat.RGBA32f, 320, 320);
            var img_RGB32i = new Image2D<int> (PixelFormat.RGBA32i, 320, 320);
            var img_RGB32ui = new Image2D<uint> (PixelFormat.RGBA32ui, 320, 320);

            var img_RGBA8 = new Image2D<float> (PixelFormat.RGB8, 320, 320);
            var img_RGBA8i = new Image2D<sbyte> (PixelFormat.RGB8i, 320, 320);
            var img_RGBA8ui = new Image2D<byte> (PixelFormat.RGB8ui, 320, 320);
            var img_RGBA16 = new Image2D<float> (PixelFormat.RGB16, 320, 320);
            var img_RGBA16f = new Image2D<float> (PixelFormat.RGB16f, 320, 320);
            var img_RGBA16i = new Image2D<short> (PixelFormat.RGB16i, 320, 320);
            var img_RGBA16ui = new Image2D<ushort> (PixelFormat.RGB16ui, 320, 320);
            var img_RGBA32f = new Image2D<float> (PixelFormat.RGB32f, 320, 320);
            var img_RGBA32i = new Image2D<int> (PixelFormat.RGB32i, 320, 320);
            var img_RGBA32ui = new Image2D<uint> (PixelFormat.RGB32ui, 320, 320);

            var img_RGB9E5 = new Image2D<float> (PixelFormat.RGB9e5, 320, 320);
            var img_R11G11B10f = new Image2D<float> (PixelFormat.R11G11B10f, 320, 320);
            var img_RGB10A2 = new Image2D<float> (PixelFormat.RGB10A2, 320, 320);
            var img_RGB10A2ui = new Image2D<ushort> (PixelFormat.RGB10A2ui, 320, 320);
            var img_Depth = new Image2D<float> (PixelFormat.Depth, 320, 320);
            

            PaintWhite (img_R8);
            PaintWhite (img_R8i);
            PaintWhite (img_R8ui);
            PaintWhite (img_R16);
            PaintWhite (img_R16f);
            PaintWhite (img_R16i);
            PaintWhite (img_R16ui);
            PaintWhite (img_R32f);
            PaintWhite (img_R32i);
            PaintWhite (img_R32ui);

            PaintWhite (img_RG8);
            PaintWhite (img_RG8i);
            PaintWhite (img_RG8ui);
            PaintWhite (img_RG16);
            PaintWhite (img_RG16f);
            PaintWhite (img_RG16i);
            PaintWhite (img_RG16ui);
            PaintWhite (img_RG32f);
            PaintWhite (img_RG32i);
            PaintWhite (img_RG32ui);

            PaintWhite (img_RGB8);
            PaintWhite (img_RGB8i);
            PaintWhite (img_RGB8ui);
            PaintWhite (img_RGB16);
            PaintWhite (img_RGB16f);
            PaintWhite (img_RGB16i);
            PaintWhite (img_RGB16ui);
            PaintWhite (img_RGB32f);
            PaintWhite (img_RGB32i);
            PaintWhite (img_RGB32ui);

            PaintWhite (img_RGBA8);
            PaintWhite (img_RGBA8i);
            PaintWhite (img_RGBA8ui);
            PaintWhite (img_RGBA16);
            PaintWhite (img_RGBA16f);
            PaintWhite (img_RGBA16i);
            PaintWhite (img_RGBA16ui);
            PaintWhite (img_RGBA32f);
            PaintWhite (img_RGBA32i);
            PaintWhite (img_RGBA32ui);

            PaintWhite (img_RGB9E5);
            PaintWhite (img_R11G11B10f);
            PaintWhite (img_RGB10A2);
            PaintWhite (img_RGB10A2ui);
            PaintBlack (img_Depth);


            img_R8.Save ("TestImageWriter(R8).png");
            img_R8i.Save ("TestImageWriter(R8i).png");
            img_R8ui.Save ("TestImageWriter(R8ui).png");
            img_R16.Save ("TestImageWriter(R16).png");
            img_R16f.Save ("TestImageWriter(R16f).png");
            img_R16i.Save ("TestImageWriter(R16i).png");
            img_R16ui.Save ("TestImageWriter(R16ui).png");
            img_R32f.Save ("TestImageWriter(R32f).png");
            img_R32i.Save ("TestImageWriter(R32i).png");
            img_R32ui.Save ("TestImageWriter(R32ui).png");

            img_RG8.Save ("TestImageWriter(RG8).png");
            img_RG8i.Save ("TestImageWriter(RG8i).png");
            img_RG8ui.Save ("TestImageWriter(RG8ui).png");
            img_RG16.Save ("TestImageWriter(RG16).png");
            img_RG16f.Save ("TestImageWriter(RG16f).png");
            img_RG16i.Save ("TestImageWriter(RG16i).png");
            img_RG16ui.Save ("TestImageWriter(RG16ui).png");
            img_RG32f.Save ("TestImageWriter(RG32f).png");
            img_RG32i.Save ("TestImageWriter(RG32i).png");
            img_RG32ui.Save ("TestImageWriter(RG32ui).png");

            img_RGB8.Save ("TestImageWriter(RGB8).png");
            img_RGB8i.Save ("TestImageWriter(RGB8i).png");
            img_RGB8ui.Save ("TestImageWriter(RGB8ui).png");
            img_RGB16.Save ("TestImageWriter(RGB16).png");
            img_RGB16f.Save ("TestImageWriter(RGB16f).png");
            img_RGB16i.Save ("TestImageWriter(RGB16i).png");
            img_RGB16ui.Save ("TestImageWriter(RGB16ui).png");
            img_RGB32f.Save ("TestImageWriter(RGB32f).png");
            img_RGB32i.Save ("TestImageWriter(RGB32i).png");
            img_RGB32ui.Save ("TestImageWriter(RGB32ui).png");

            img_RGBA8.Save ("TestImageWriter(RGBA8).png");
            img_RGBA8i.Save ("TestImageWriter(RGBA8i).png");
            img_RGBA8ui.Save ("TestImageWriter(RGBA8ui).png");
            img_RGBA16.Save ("TestImageWriter(RGBA16).png");
            img_RGBA16f.Save ("TestImageWriter(RGBA16f).png");
            img_RGBA16i.Save ("TestImageWriter(RGBA16i).png");
            img_RGBA16ui.Save ("TestImageWriter(RGBA16ui).png");
            img_RGBA32f.Save ("TestImageWriter(RGBA32f).png");
            img_RGBA32i.Save ("TestImageWriter(RGBA32i).png");
            img_RGBA32ui.Save ("TestImageWriter(RGBA32ui).png");

            
            img_RGB9E5.Save("TestImageWriter(RGB9E5).png");
            img_R11G11B10f.Save("TestImageWriter(R11G11B10f).png");
            img_RGB10A2.Save("TestImageWriter(RGB10A2).png");
            img_RGB10A2ui.Save("TestImageWriter(RGB10A2ui).png");
            img_Depth.Save ("TestImageWriter(Depth).png");
        }
        #endregion
    }
}